export default class Http3DReq {
    public static req3Durl = "";
    constructor(){
    }

    public static async warnList()
    {
        return this.GET("warn/all/list",{});
    }

    public static async warnlistgetbytype(params){
        return this.GET("warn/list/getbytype",params);
        
    }
    public static warnlogListHistory(params:any){
        return this.GET("warnlog/list",params);
    }
    public static warnlogListToday(params:any){
        return this.GET("warnlog/list/todayPage",params);
    }
    public static warnRtspList(params:any){
        return this.GET("warn/rtsp/list",params);
    }
    public static warnLogVerify(params){
        return this.GET("warnlog/verify",params);
    }

    public static sensorList(){
        return this.GET("sensor/list",{});
    }

    public static async GET(api: string, params: object = {}, nowUrl: string = "") {
        return new Promise((resolve) => {
            if(!Http3DReq.req3Durl)
            Http3DReq.req3Durl = window["req3Durl"];
            if (nowUrl == "")
                nowUrl = Http3DReq.req3Durl + "/" + api;

            if (params) {
                let paramsArray = [];
                //拼接参数
                Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))

                if (paramsArray.length > 0)
                    nowUrl += ('?' + paramsArray.join('&'));
            }
            //fetch请求
            fetch(nowUrl, {
                method: 'GET',
            })
                .then((response) => response.json())
                .then((json) => {
                    this.dealData(json, resolve, api);
                    // console.log("戴假发"+JSON.stringify(json));
                    // this.setState({ discounts: json.data })
                })
                .catch((error) => {
                    // alert(error)
                    console.error("请求服务器失败：" + api);
                    resolve(null);
                })
        });

    }
    private static dealData(jsonObj: any, resolve: any, api: string) {
        if (jsonObj) {
            if (jsonObj.message && jsonObj.message == "success") {
                if (jsonObj.data)
                    resolve(jsonObj.data);
                else
                    resolve("success");
                return;
            }
            console.error('请求失败' + jsonObj.message);
            resolve(null);
            return;
        }
        console.error('请求失败');
        resolve(null);
        return;
    }

}